﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ProjectPigs.Util
{
    public class Timer
    {
        long duration;

        long elapsedTime;
        long startTime;
        Boolean locked;

        Boolean isRunning;



        public Timer(long duration)
        {
            this.duration = duration;
        }

        public Boolean IsRunning
        {
            get
            {
                return isRunning;
            }
        }

        public long Duration
        {
            get
            {
                return duration;
            }
            set
            {
                duration = value;
            }
        }

        public float Percentage
        {
            get
            {
                return (float)getCurrentMillis() / (float)Duration;
            }
        }
        public Boolean eval(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime.Milliseconds;

            if (elapsedTime > duration)
            {
                return true;
            }
            return false;
        }

        public Boolean once(GameTime gameTime)
        {
            isRunning = false;
            elapsedTime += gameTime.ElapsedGameTime.Milliseconds;

            if (elapsedTime > duration && !locked)
            {
                locked = true;
                return true;
            }
            return false;
        }

        public Boolean repeat(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime.Milliseconds;

            if (elapsedTime > duration)
            {
                elapsedTime = 0L;
                return true;
            }
            return false;
        }

        public void updateElapsedTime(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime.Milliseconds;
        }

        public void start()
        {
            elapsedTime = 0L;
            locked = false;
            isRunning = true;
        }

        public void stop()
        {
            elapsedTime = 0L;
            locked = false;
            isRunning = false;
        }

        public long getCurrentMillis()
        {
            return elapsedTime;
        }

    }
}
